import { getToken, setToken } from '@/utils/auth'
import { login, getUserInfo } from '@/api/user'
const state = {
  token: getToken() || '',
  userInfo: {}
}

const mutations = {
  setToken: function(state, newToken) {
    state.token = newToken
  },
  setUserInfo: function(state, info) {
    state.userInfo = info
  }
}

const actions = {
  logout(context) {
    context.commit('setToken', '')
    context.commit('setUserInfo', {})
  },
  async  getInfo(context) {
    const res = await getUserInfo()
    // console.log(res)
    context.commit('setUserInfo', res.data)
  },

  async login(context, payload) {
    const res = await login(payload)
    // console.log(res)
    setToken(res.data)
    context.commit('setToken', res.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
